package cn.tedu.coolsharkmall.web;

import cn.tedu.coolsharkmall.ex.ServiceException;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult<T> implements Serializable {
    /**
     * 操作结果的状态码
     */
    @ApiModelProperty("业务状态码")
    private Integer state;
    /**
     * 操作结果的提示文本
     */
    @ApiModelProperty("提示文本")
    private String message;
    @ApiModelProperty("相应数据")
    private T data;

    public static JsonResult<Void> ok(){
        return ok(null);
    }

    public static<T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.state = ServiceCode.OK.getValue();
        jsonResult.data = data;
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e){
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode state, String message){
        JsonResult<Void> jsonResult = new JsonResult();
        jsonResult.state = state.getValue();
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
